package com.ruitu.health_1.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.ruitu.arad.base.base_list.BaseRefreshActivity;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.support.widget.dialog.CustomAlertDialog;
import com.ruitu.arad.support.widget.progress.ProgressLayout;
import com.ruitu.health_1.R;
import com.ruitu.health_1.mvp.model.UserInfoModel;
import com.ruitu.health_1.mvp.presenter.UserInfoPresent;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.user.Address;
import com.ruitu.router_module.util.ReqUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddressActivity extends BaseRefreshActivity<UserInfoPresent.AddressListP, UserInfoModel.AddressM> {
    private DataAdapter adapter;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_address;
    }

    private String from;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHeadTitle("地址管理");
        Bundle extras = getIntent().getExtras();
        if (extras!= null) {
            from = (String) extras.get("from");
        }
        setLoadMoreEnabled(false);
    }

    private void getData() {
        p.addressList(AppHolder.instance().getUser().getId());
    }

    public void deleteSuc() {
        dialog.cancel();
        getData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        showProgress();
        getData();
    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new DataAdapter(this);
        return adapter;
    }

    private LRecyclerView l_recyclerview;

    @Override
    protected LRecyclerView findRecyclerView() {
        l_recyclerview = findViewById(R.id.l_recyclerview);
        return l_recyclerview;
    }

    @Override
    protected void reqFirst() {
        getData();
    }

    @Override
    protected void reqMore() {

    }

    @Override
    protected void initRecyclerView() {
        super.initRecyclerView();

    }

    public void setData(List<Address> addressList) {
        refreshComplete();
        adapter.setDataList(addressList);
    }

    public void onAdAddress(View view) {
        startActivity(AdAdActivity.class);
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return null;
    }

    private CustomAlertDialog dialog;

    class DataAdapter extends ListBaseAdapter<Address> {
        public DataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_address;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_name = holder.getView(R.id.tv_name);
            TextView tv_tel = holder.getView(R.id.tv_tel);
            TextView tv_area = holder.getView(R.id.tv_area);
            TextView tv_detailed_address = holder.getView(R.id.tv_detailed_address);
            TextView tv_address_type = holder.getView(R.id.tv_address_type);
            LinearLayout ll_item = holder.getView(R.id.ll_item);
            final Address address = mDataList.get(position);
            tv_name.setText(address.getName());
            String phone = address.getPhoneNumber();
            if (!TextUtils.isEmpty(phone)) {
//                String newAddress = phone.substring(0, 3) + "****" + phone.substring(6, 10);
                tv_tel.setText(phone);
            }
            if (TextUtils.equals(address.getDefaultStatus(), "1")) {
                tv_address_type.setVisibility(View.VISIBLE);
            } else {
                tv_address_type.setVisibility(View.GONE);
            }
            tv_area.setText(address.getProvince() + " " + address.getCity() + " " + address.getRegion());
            tv_detailed_address.setText(address.getDetailAddress());

            ll_item.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    dialog = new CustomAlertDialog(mContext, "是否删除该地址?", new CustomAlertDialog.OkOnClickListener() {
                        @Override
                        public void OkOnClick() {
                            Map<String, String> map = new HashMap<>();
                            map.put("id", address.getId());
                            map.put("memberId", AppHolder.instance().getUser().getId());
                            showProgress();
                            p.deleteAddress(ReqUtil.getBody(map, true));
                        }
                    });
                    return false;
                }
            });

            ll_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (TextUtils.equals(from,"from")) {
                        Intent intent = new Intent(AddressActivity.this, CommitOrderActivity.class);
                        intent.putExtra("address", address);
                        setResult(RESULT_OK, intent);
                        finish();
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("address", address);
                        startActivity(AdAdActivity.class, bundle);
                    }
                }
            });
        }
    }
}
